package com.jianyun.jyzs.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.jianyun.jyzs.bean.EnNatureResponse;
import com.jianyun.jyzs.db.DatabaseHelper;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by liml on 17/8/8.
 */

public class EnNatureDao {
    private Dao<EnNatureResponse.EnNatureBean, Integer> enNatureDao;
    private DatabaseHelper dbHelper;

    /**
     * 构造方法
     * 获得数据库帮助类实例，通过传入Class对象得到相应的Dao
     */
    public EnNatureDao(Context context) {
        try {
            dbHelper = DatabaseHelper.getHelper(context);
            enNatureDao = dbHelper.getDao(EnNatureResponse.EnNatureBean.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void insert(EnNatureResponse.EnNatureBean bean, String en){
        try {
            bean.setEnterpriseCode(en);
            enNatureDao.createOrUpdate(bean);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public List<EnNatureResponse.EnNatureBean> getEnNatureList(String en){
        QueryBuilder<EnNatureResponse.EnNatureBean, Integer> builder = enNatureDao.queryBuilder();
        try {
            Where<EnNatureResponse.EnNatureBean, Integer> where = builder.where().eq("enterpriseCode", en);
            List<EnNatureResponse.EnNatureBean> query = where.query();
            return query;
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void clear(String enterpriseCode) {
        DeleteBuilder<EnNatureResponse.EnNatureBean, Integer> deleteBuilder = enNatureDao.deleteBuilder();
        try {
            deleteBuilder.where().eq("enterpriseCode",enterpriseCode);
            deleteBuilder.delete();
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }

    public List<EnNatureResponse.EnNatureBean> getEnNatureSearch(String enterpriseCode, String trim) {
        QueryBuilder<EnNatureResponse.EnNatureBean, Integer> builder = enNatureDao.queryBuilder();
        try {
            builder.where().eq("enterpriseCode",enterpriseCode).and().like("name","%"+trim+"%");
            return builder.query();
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<String> getEnNatureList2(String enterpriseCode) {
        QueryBuilder<EnNatureResponse.EnNatureBean, Integer> builder = enNatureDao.queryBuilder();
        List<String> list = new ArrayList<>();
        try {
            Where<EnNatureResponse.EnNatureBean, Integer> where = builder.where().eq("enterpriseCode", enterpriseCode);
            List<EnNatureResponse.EnNatureBean> query = where.query();
            for (EnNatureResponse.EnNatureBean ee:query
                 ) {
                list.add(ee.getName());
            }
            return list;
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
            return list;
        }
    }
}
